package com.anxin.act.task.service;

import com.alibaba.fastjson.JSONObject;
import com.anxin.act.task.dao.ViewRuTaskDao;
import com.anxin.act.task.entity.ViewRuTask;
import com.anxin.act.utils.ActProcessUtils;
import com.anxin.act.utils.ActUtils;
import com.anxin.common.utils.DateUtils;
import com.anxin.common.utils.dict.DictUtils;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.framework.redis.MqProducer;
import com.anxin.framework.redis.RedisMq;
import com.anxin.framework.security.entity.SysTempToken;
import com.anxin.framework.security.utils.SysTempTokenUtils;
import com.anxin.sys.message.utils.SendMessageUtils;
import com.anxin.sys.system.entity.SysUser;
import com.anxin.sys.system.utils.ConfigUtils;
import org.activiti.engine.history.HistoricProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 待办提醒
 */
@Service
public class ActSendTaskService implements RedisMq {
    @Autowired
    private MqProducer mqProducer;
    @Autowired
    private ViewRuTaskDao viewRuTaskDao;
    @Autowired
    private ActDockingService actDockingService;
    @Autowired
    private WfPushLogService wfPushLogService;

    /**
     * 发送待办提醒
     * @param actId 流程实例ID
     * @param type 提醒类型 待办:todoTask催办：hastenwork
     */
    public void sendTask(String actId,String type){
        JSONObject message = new JSONObject();
        message.put("actId",actId);
        message.put("type",type);

        mqProducer.sendMessage(ActSendTaskService.class,message);
    }

    @Override
    public boolean onMessage(String message) {
        JSONObject dataJson = JSONObject.parseObject(message);
        // 流程实例ID
        String actId = dataJson.getString("actId");
        // 加签类型
        String type = dataJson.getString("type");

        Map<String, Object> processVar = ActUtils.getProcessVar(actId);
        HistoricProcessInstance hiProcessInstance = ActProcessUtils.getHiProcessInstance(actId);
        JSONObject messageJson = new JSONObject();
        messageJson.put("applyNickName",processVar.get("applyNickName"));
        messageJson.put("applyTime", DateUtils.format(new Date(Long.parseLong(processVar.get("applyTime").toString()))));
        messageJson.put("urgent", DictUtils.getDictLabel(processVar.get("urgent").toString(),"sys_yes_no",""));
        messageJson.put("title",processVar.get("title"));
        messageJson.put("processName",hiProcessInstance.getProcessDefinitionName());


        List<ViewRuTask> viewRuTaskList = viewRuTaskDao.findListByActId(actId);

        for (ViewRuTask viewRuTask : viewRuTaskList) {
            if (!"Y".equals(viewRuTask.getHang())){
                // 查询是否发送过提醒
                SysUser sysUser = UserUtils.getUserByUserName(viewRuTask.getAssignee());if (sysUser != null){
                    Integer sendCount = wfPushLogService.getTodoCountByTaskId(viewRuTask.getId(),sysUser.getId());
                    if (sendCount == 0){
                        // 待办对接
                        SysTempToken sysTempToken = actDockingService.sendTask(viewRuTask, sysUser.getId());
                        if(sysTempToken == null){
                            sysTempToken = SysTempTokenUtils.saveToken(sysUser.getId(), viewRuTask.getId(), "/unifiedTask?taskId=" + viewRuTask.getId());
                        }

                        messageJson.put("pcAddress", ConfigUtils.get("web_url")+sysTempToken.getUseUrl());
                        messageJson.put("appAddress", ConfigUtils.get("mobile_url")+sysTempToken.getUseUrl());
                        messageJson.put("currentTask",viewRuTask.getTaskName());

                        // 发送待办提醒
                        SendMessageUtils.sendMessage(type, sysUser.getId(), messageJson);
                    }
                }
            }


        }

        return true;
    }
}
